<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="患者编号" prop="patId">
        <el-input v-model="formData.patId" placeholder="请输入患者编号" />
      </el-form-item>
      <el-form-item label="患者姓名" prop="patName">
        <el-input v-model="formData.patName" placeholder="请输入患者姓名" />
      </el-form-item>
      <el-form-item label="患者性别" prop="patGender">
        <el-radio-group v-model="formData.patGender">
          <el-radio :value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="预约时间段" prop="bookTimeslot">
        <el-input v-model="formData.bookTimeslot" placeholder="请输入预约时间段" />
      </el-form-item>
      <el-form-item label="预约检查类型" prop="bookCheckType">
        <el-radio-group v-model="formData.bookCheckType">
          <el-radio :value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排队序号" prop="seqNum">
        <el-input v-model="formData.seqNum" placeholder="请输入排队序号" />
      </el-form-item>
      <el-form-item label="5:过号 10:排队中 15:已召回 20:候诊准备 30:就诊中 40:就诊完成" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :value="10">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="过号标记" prop="passed">
        <el-radio-group v-model="formData.passed">
          <el-radio :value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="预约过期标记" prop="expired">
        <el-radio-group v-model="formData.expired">
          <el-radio :value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="诊室编号" prop="roomId">
        <el-input v-model="formData.roomId" placeholder="请输入诊室编号" />
      </el-form-item>
      <el-form-item label="诊疗床编号" prop="bedNo">
        <el-input v-model="formData.bedNo" placeholder="请输入诊疗床编号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { QueueApi, QueueVO } from '@/api/ecg/queue'

/** 排队 表单 */
defineOptions({ name: 'queueForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  patId: undefined,
  patName: undefined,
  patGender: undefined,
  bookTimeslot: undefined,
  bookCheckType: undefined,
  seqNum: undefined,
  status: undefined,
  passed: undefined,
  expired: undefined,
  roomId: undefined,
  bedNo: undefined
})
const formRules = reactive({
  patId: [{ required: true, message: '患者编号不能为空', trigger: 'blur' }],
  patName: [{ required: true, message: '患者姓名不能为空', trigger: 'blur' }],
  bookTimeslot: [{ required: true, message: '预约时间段不能为空', trigger: 'blur' }],
  seqNum: [{ required: true, message: '排队序号不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '5:过号 10:排队中 15:已召回 20:候诊准备 30:就诊中 40:就诊完成 ', trigger: 'blur' }],
  passed: [{ required: true, message: '过号标记不能为空', trigger: 'blur' }],
  expired: [{ required: true, message: '预约过期标记不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await QueueApi.getqueue(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as QueueVO
    if (formType.value === 'create') {
      await QueueApi.createqueue(data)
      message.success(t('common.createSuccess'))
    } else {
      await QueueApi.updatequeue(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    patId: undefined,
    patName: undefined,
    patGender: undefined,
    bookTimeslot: undefined,
    bookCheckType: undefined,
    seqNum: undefined,
    status: undefined,
    passed: undefined,
    expired: undefined,
    roomId: undefined,
    bedNo: undefined
  }
  formRef.value?.resetFields()
}
</script>
